<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员等级
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'等级列表'),
			array('op'=>'edit','title'=>'添加等级'),
	);

//等级列表
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','level','title','ordermoney','ordercount','discount','status','isup');
	$list=pdo_getslice('jy_weishop_member_level',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
		}
	}
	//添加默认等级
	$default_level=array(
		'id'=>'0',
		'title'=>'默认会员',
		'level'=>0,
		'discount'=>'0',
		'ordermoney'=>'0.00',
		'ordercount'=>'0',
		'status'=>'1',
	);
	array_unshift($list,$default_level);
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'level','title'=>'等级','type'=>'string'),
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'discount','title'=>'折扣','type'=>'string'),
			array('name'=>'ordermoney','title'=>'升级条件（订单金额满）','type'=>'string'),
			array('name'=>'ordercount','title'=>'升级条件（订单数量满）','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'等级名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'启用');
	$return['status2']=array(array('id'=>0,'title'=>'关闭'),array('id'=>1,'title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_member_level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$item['goods_ids']=$item['goods_ids']?json_decode($item['goods_ids'],true):array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回等级列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'select','title'=>'等级权重','name'=>'level','options'=>'levels','option_value'=>'level','help'=>'权重数字越大等级越高，等级权重必须独立，不能跟其他等级的权重一样！'),
					array('type'=>'string','title'=>'等级名称','name'=>'title','help'=>''),
					array('type'=>'switch','title'=>'是否开启自动升级','name'=>'isup','help'=>'开启自动升级，用户触发下面的条件，才会自动升级会员'),
					array('type'=>'radio','title'=>'触发升级时机','name'=>'up_type','options'=>'up_type','help'=>''),
					array('type'=>'choose_many','title'=>'购买指定商品升级','name'=>'goods_ids','action'=>'/goods.get_goods/many/','choose_title'=>'选择参与的商品','help'=>'（多个商品时，任意其中一个）注意：注意触发升级时机，需核销的商品，只有核销后才会完成！虚拟商品，发货后自动会完成！'),
					array('type'=>'string','title'=>'升级条件（订单金额满）','name'=>'ordermoney','help'=>'注意触发升级时机，注意：后台手动强制修改订单状态，不会触发！'),
					array('type'=>'string','title'=>'升级条件（订单数量满）','name'=>'ordercount','help'=>'注意触发升级时机，注意：后台手动强制修改完成状态，不会触发！'),
					array('type'=>'string','title'=>'折扣(比)','name'=>'discount','help'=>'请输入0.1~10之间的数字,值为空代表不设置折扣；例如8，是指8折'),
					array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),

				)
			),
		)
	);
	$return['up_type']=array('订单完成','订单支付');
	$levels=array();
	for($i=1;$i<=100;$i++){
		$levels[]=array('level'=>$i,'title'=>$i.'级');
	}
	$return['levels']=$levels;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['level']){
		exi('等级权重必选！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'level'=>intval($postdata['level']),
		'isup'=>intval($postdata['isup']),
		'up_type'=>intval($postdata['up_type']),
		'ordercount'=>intval($postdata['ordercount']),
		'ordermoney'=>round($postdata['ordermoney'],2),
		'discount'=>round($postdata['discount'],2),
		'status'=>intval($postdata['status']),
		'goods_ids'=>ijson_encode($postdata['goods_ids']),
	);
	$isadd=pdo_get('jy_weishop_member_level',array('level'=>intval($postdata['level']),'uniacid'=>$_SESSION['uniacid']));
	$id=intval($postdata['id']);
	if($id){
		if($id != $isadd['id'] && $isadd['id']){
			exi('该等级权重已经添加了，请换一个！','error');
		}
		pdo_update('jy_weishop_member_level',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		if($isadd['id']){
			exi('该等级权重已经添加了，请换一个！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_member_level',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(!$_RQ['id']){
		exi('默认等级不能删除！','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$issetid=pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'level'=>$id),'id');
			if($issetid){
				exi('该会员等级下面已经有会员，不能删除！','error');
			}
			$issetid=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'showlevels like'=>'%'.$id.'%'),'id');
			if($issetid){
				exi('该会员等级下面已经有商品绑定，不能删除！','error');
			}
			pdo_delete('jy_weishop_member_level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择等级！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_weishop_member_level',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}